#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "mywidgetfactory.h"
#include "mytcpproxy.h"
#include <QMessageBox>
#include <QDebug>

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

//登录按钮点击事件
void LoginWidget::on_pushButton_clicked()
{
    //登录验证
    if(!MyTcpProxy::sndLogin(ui->input_user->text(),ui->input_pwd->text())){
        QMessageBox::critical(this,"登录失败","可能通信发生故障");
    }
}

//注册按钮点击事件
void LoginWidget::on_pushButton_2_clicked()
{
    this->hide();
    MyWidgetFactory::getRegistWidget()->show();
}

void LoginWidget::onLoginRet(const bool &ret, const QString &msg)
{
    qDebug() << "ret="<<ret<<", msg="<<msg;
    if(ret == true){
        QMessageBox::information(this,"登录成功","登录成功!");
        //界面跳转
        this->hide();
        MyWidgetFactory::getMainWidget()->show(ui->input_user->text());
    }else{
        QMessageBox::critical(this,"登录失败","用户名或密码错误");
    }
}
